package model

import (
	"fmt"
	"lutai/src/schema"
	"lutai/src/service/db"
)

// Packet db
type Packet struct {
	db *db.DB
}

// Init 初始化
func (p *Packet) Init(db *db.DB) *Packet {
	db.AddTableWithName(schema.Packet{}, schema.PacketTable)
	p.db = db
	return p
}

// QueryPage 查询
func (p *Packet) QueryPage(page, pageSize int) ([]*schema.Packet, error) {
	var ps []*schema.Packet
	sql := fmt.Sprintf("select * from %s order by id desc limit ?,?", schema.PacketTable)
	var value []interface{}
	value = append(value, db.GetLimitOffset(page, pageSize), pageSize)

	_, err := p.db.Select(&ps, sql, value...)
	return ps, err
}

// QueryCount 查询
func (p *Packet) QueryCount() (count int, err error) {
	sql := fmt.Sprintf("select count(*) Count from %s", schema.PacketTable)
	err = p.db.SelectOne(&count, sql)
	return count, err
}

// GetPacketData 获取包数据
func (p *Packet) GetPacketData(id string) (data schema.Packet, err error) {
	sql := fmt.Sprintf("select pkg_type,data from %s where id=?", schema.PacketTable)
	err = p.db.SelectOne(&data, sql, id)
	return
}
